﻿
'Description:自定义页面基类
'Copyright (c) : 通力凯顿（北京）系统集成有限公司
'Writer:Yangsj
'create Date:2017-01-29
'Rewriter:
'Rewrite Date:

Imports Microsoft.VisualBasic
Imports System.Drawing
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls
Imports Camstar.WebClient.FormsBuilder.WebForm
Imports BusinessRule.DTO
Imports Camstar.WebClient.FormsBuilder.WebControls

Namespace InSiteWebApplication


    Public Class uLMSForm
        Inherits Page

        Protected Overrides Sub OnInit(ByVal e As System.EventArgs)
            MyBase.OnInit(e)
            HeaderAddLink("/InSiteWebApplication/styles/FormObjects.css")
            HeaderAddLink("/InSiteWebApplication/styles/Layout.css")
            HeaderAddLink("/InSiteWebApplication/styles/LayoutWorkingSection.css")
            HeaderAddLink("/InSiteWebApplication/styles/Images.css")
            HeaderAddLink("/InSiteWebApplication/styles/Text.css")
            HeaderAddLink("/InSiteWebApplication/styles/TreeView.css")
            HeaderAddLink("/InSiteWebApplication/styles/User.css")

            HeaderAddScript("/InSiteWebApplication/javascript/SinglePane.js")
            HeaderAddScript("/InSiteWebApplication/javascript/InSiteForm.js")
            HeaderAddScript("/InSiteWebApplication/javascript/Common.js")
            HeaderAddScript("/InSiteWebApplication/javascript/WorkingSectionCommon.js")
            HeaderAddScript("/InSiteWebApplication/javascript/Grid.js")
            HeaderAddScript("/InSiteWebApplication/javascript/Form.js")
            HeaderAddScript("/InSiteWebApplication/javascript/TreeView.js")
            HeaderAddScript("/InSiteWebApplication/javascript/Customer.js")
        End Sub

        Protected Overridable Sub OnPageLoad(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim UserInfo As CurrentUserInfo = Session("CurrentUserInfo")
            Dim script As String
            If IsNothing(UserInfo) Then
                script = String.Format("<script> ValidateSessionID(''); </script> ")
            Else
                script = String.Format("<script> ValidateSessionID('{0}'); </script> ", UserInfo.SessionID)
            End If
            ClientScript.RegisterStartupScript(Me.GetType(), "NotifyParent", script)
            ClientScript.RegisterStartupScript(Me.GetType(), "menu", "<script> document.oncontextmenu = function () { return false }</script>")
            ClientScript.RegisterStartupScript(Me.GetType(), "F5", "<script> function document.onkeydown() { if ( event.keyCode==116) { event.keyCode = 0; event.cancelBubble = true; return false; } } </script>")
            Response.Buffer = True
            Response.ExpiresAbsolute = DateTime.Now.AddSeconds(-1)
            Response.Expires = 0
            Response.CacheControl = "no-cache"
        End Sub

        Public Sub HeaderAddScript(ByVal src As String)
            Dim script As New HtmlGenericControl("script")
            script.Attributes("language") = "javascript"
            script.Attributes("Type") = "text/javascript"
            script.Attributes("Src") = src
            Header.Controls.Add(script)
        End Sub

        Public Sub HeaderAddLink(ByVal href As String)
            Dim link As New HtmlGenericControl("link")
            link.Attributes("rel") = "stylesheet"
            link.Attributes("href") = href
            Header.Controls.Add(link)
        End Sub

        Public Sub DisplayMessage(ByVal msgCtrl As System.Web.UI.WebControls.Label, ByVal msg As String, ByVal isOK As Boolean)
            If isOK Then
                msgCtrl.Style.Item("Color") = "black"
                msgCtrl.Text = "消息:" + msg
            Else
                msgCtrl.Style.Item("Color") = "Red"
                msgCtrl.Text = "消息:" + msg
            End If
        End Sub

        ''' <summary>
        ''' 调用弹出页面传值
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Property PopupData As Object
            Get
                If IsNothing(Session("PopupData")) Then
                    Return Nothing
                Else
                    Return Session("PopupData")
                End If
            End Get
            Set(ByVal value As Object)
                Session("PopupData") = value
            End Set
        End Property

        ''' <summary>
        ''' 弹出页面方法
        ''' </summary>
        ''' <param name="linkbtn"></param>
        ''' <param name="isPopup">true弹出，false弹出新页面</param>
        ''' <remarks></remarks>
        Public Sub ShowPopupPage(ByVal linkbtn As PopupLinkButton, ByVal isPopup As Boolean)
            Dim key As String = linkbtn.Text
            Dim script As String
            Dim url = linkbtn.PageName
            If url.Contains("?") Then
                url += "&v="
            Else
                url += "?v="
            End If
            If isPopup Then
                script = String.Format("<script>OpenPopupWindow(false,'{0}'+new Date().getTime(),'dialogWidth={1}px;dialogHeight={2}px;status=0');</script>", url, linkbtn.PopupWidth, linkbtn.PopupHeight)
            Else
                script = String.Format("<script>window.open('{0}'+new Date().getTime());</script>", url)
            End If
            ClientScript.RegisterStartupScript(Me.GetType(), key, script)
        End Sub

        Public Sub ClearMessage()

        End Sub
        ''' <summary>
        ''' 获取用户名
        ''' </summary>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Function GetUserName() As String
            Dim UserInfo As CurrentUserInfo = Session("CurrentUserInfo")
            GetUserName = UserInfo.APIUserName
        End Function
        ''' <summary>
        ''' 获取用户密码
        ''' </summary>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Function GetPassword() As String
            Dim UserInfo As CurrentUserInfo = Session("CurrentUserInfo")
            GetPassword = UserInfo.APIPassword
        End Function
    End Class

    '<summary>
    '名称：SubmitOncePage 
    '父类：System.Web.UI.Page
    '描述：解决浏览器刷新造成的数据重复提交问题的page扩展类。
    Public Class SubmitOncePage
        Inherits Web.UI.Page
        Private _strSessionKey As String
        Private _hiddenfieldName As String
        Private _strLastViewstate As String

        Public Sub SubmitOncePage()
            _hiddenfieldName = "__LastVIEWSTATE_SessionKey"
            _strSessionKey = System.Guid.NewGuid().ToString()
            _strLastViewstate = String.Empty
        End Sub

        Public ReadOnly Property IsRefreshed As Boolean
            Get
                Dim str1 As String = GetSessinContent()
                _strLastViewstate = str1
                Dim str2 As String = Me.Session(GetSessinKey()).ToString
                If (str1 IsNot Nothing) & (str2 IsNot Nothing) & (str1 = str2) Then
                    Return True
                Else
                    Return False
                End If
            End Get

        End Property

        Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
            Dim str = GetSessinKey()
            Me.Session(str) = _strLastViewstate
            ClientScript.RegisterHiddenField(_hiddenfieldName, str)
            MyBase.Render(writer)
        End Sub

        Private Function GetSessinKey() As String
            Dim str = Me.Request.Form(_hiddenfieldName)
            Return If(str = Nothing, _strSessionKey, str)
        End Function

        Private Function GetSessinContent() As String
            Dim str = Me.Request.Form("__VIEWSTATE")
            If IsNothing(str) Then
                Return Nothing
            End If
            Return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5")

        End Function
    End Class

End Namespace


